using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;

namespace MainGUI
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            m_mainForm = new Form1();
            Application.Run(m_mainForm);
        }

        static public String CarInfo(string car, string ip)
        {
            String str = null;
            StreamWriter sw = new StreamWriter("input.txt");
            sw.WriteLine(ip + " " + car);
            sw.Close();

            try {
                UIWrapper.SendCarMessage();
            }
            catch (Exception ex){
                MessageBox.Show("Exception thrown: " + ex.Message + ".", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return "";
            }

            try {
                File.Delete("input.txt");
            }
            catch (Exception ex) {
                return "";
            }

            StreamReader sr;
            try {
                sr = new StreamReader("output.txt");
            }
            catch (Exception ex) {
                return "";
            }

            str = "";
            String read = sr.ReadLine();
            while (!sr.EndOfStream) {
                if (read != "")
                    str += read + "\r\n";

                read = sr.ReadLine();
            }
             
            sr.Close();
            File.Delete("output.txt");

            return str;
        }

        static public String PLotInfo(string ip) 
        {
            String str = null;
            StreamWriter sw = new StreamWriter("input.txt");
            sw.WriteLine(ip);
            sw.Close();

            try {
                UIWrapper.SendPLotMessage();
            }
            catch {
                MessageBox.Show("Exception thrown in PLotInfo.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return "";
            }

            try {
                File.Delete("input.txt");
            }
            catch (Exception ex) {
                return "";
            }
            StreamReader sr;
            try {
                sr = new StreamReader("output.txt");
            }
            catch (Exception ex) {
                return "";
            }


            str = "";
            String read = sr.ReadLine();
            while (!sr.EndOfStream) {
                if (read != "")
                    str += read + "\r\n";

                read = sr.ReadLine();
            }

            sr.Close();
            File.Delete("output.txt");

            return str;
        }

        static Form m_mainForm = null;
    }
}